package com.weilive.push.gate;

import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.weilive.core.netty.http.HttpServlet;
import com.weilive.core.utils.QueryStringDecoder;
import com.weilive.push.service.PusherService;

@Service
public class PusherServlet extends HttpServlet {

	private static final Logger logger = LoggerFactory.getLogger(PusherServlet.class);
	@Autowired
	private PusherService apnsService;

	@Override
	public String getServletPath() {
		return "/push";
	}

	@Override
	public HttpResponse processRequest(HttpRequest request) throws Exception {

		QueryStringDecoder decoder = decodeQueryString(request);

		long userId = decoder.getLong("userId", -1);
		int type = decoder.getInt("type", -1);

		if (userId < 0 || type < 0) {
			return createJsonResponse("fail");
		}

		logger.warn("userId={}, type={}", new Object[] { userId, type });

		apnsService.push(userId, type);

		return createJsonResponse("ok");
	}
}